home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / buffer.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  15KB  |  431 lines

  1. /*
  2.  * Buffer management.
  3.  * Some of the functions are internal,
  4.  * and some are actually attached to user
  5.  * keys. Like everyone else, they set hints
  6.  * for the display system.
  7.  */
  8. #include        <stdio.h>
  9. #include        "ed.h"
  10.  
  11. /* USEBUFFER eXtended command.  Prompt for buffer name.  Store it in
  12.  * external pattern lastbuf.  Call selbuf() to make the change.  Bound
  13.  * to CTLX-B.
  14.  */
  15. usebuffer(f, n)
  16. register int f, n;
  17. {
  18.         register int    s;
  19.  
  20.         if ((s=readpattern("Use buffer [DEFAULT] ", &lastbuf)) != TRUE)
  21.                 return (s);
  22.         return(selbuf(lastbuf));
  23. }
  24.  
  25. /*
  26.  * Attach a buffer to a window. The
  27.  * values of dot and mark come from the buffer
  28.  * if the use count is 0. Otherwise, they come
  29.  * from some other window.
  30.  */
  31. selbuf(bufname)
  32. register char *bufname;
  33. {
  34.         register BUFFER *bp;
  35.         register WINDOW *wp;
  36.  
  37.         /* Find a buffer.  If the buffer does not exist, then ask the user
  38.          * whether a new buffer should be created.
  39.          */
  40.  
  41.         if ((bp=bfind(bufname, MAYBE, 0)) == NULL)
  42.                 return (FALSE);
  43.         strcpy(lastbuf, curbp->b_bname);        /* set up for return    */
  44.         if (--curbp->b_nwnd == 0) {             /* Last use.            */
  45.                 curbp->b_dotp  = curwp->w_dotp;
  46.                 curbp->b_doto  = curwp->w_doto;
  47.                 curbp->b_markp = curwp->w_markp;
  48.                 curbp->b_marko = curwp->w_marko;
  49.         }
  50.         curbp = bp;                             /* Switch.              */
  51.         curwp->w_bufp  = bp;
  52.         curwp->w_linep = bp->b_linep;           /* For macros, ignored. */
  53.         curwp->w_flag |= WFMODE|WFFORCE|WFHARD; /* Quite nasty.         */
  54.         if (bp->b_nwnd++ == 0) {                /* First use.           */
  55.                 curwp->w_dotp  = bp->b_dotp;
  56.                 curwp->w_doto  = bp->b_doto;
  57.                 curwp->w_markp = bp->b_markp;
  58.                 curwp->w_marko = bp->b_marko;
  59.                 return (TRUE);
  60.         }
  61.         wp = wheadp;                            /* Look for old.        */
  62.         while (wp != NULL) {
  63.                 if (wp!=curwp && wp->w_bufp==bp) {
  64.                         curwp->w_dotp  = wp->w_dotp;
  65.                         curwp->w_doto  = wp->w_doto;
  66.                         curwp->w_markp = wp->w_markp;
  67.                         curwp->w_marko = wp->w_marko;
  68.                         break;
  69.                 }
  70.                 wp = wp->w_wndp;
  71.         }
  72.         return (TRUE);
  73. }
  74.  
  75. /* KILLBUFFER  eXtended command.  Prompt for buffer name.  Call delbuf()
  76.  * to do the actual kill.  Bound to CTLX-K.
  77.  */
  78. killbuffer(f, n)
  79. register int f, n;
  80. {
  81.         register int    s;
  82.  
  83.         if ((s=readpattern("Kill buffer [DEFAULT] ", &lastbuf)) != TRUE)
  84.                 return (s);
  85.         return(delbuf(lastbuf));
  86. }
  87.  
  88. /*
  89.  * Dispose of a buffer, by name.
  90.  * Look up bufname (don't get too
  91.  * upset if it isn't there at all!). Get quite upset
  92.  * if the buffer is being displayed. Clear the buffer (ask
  93.  * if the buffer has been changed). Then free the header
  94.  * line and the buffer header.
  95.  */
  96. delbuf(bufname)
  97. register char *bufname;
  98. {
  99.         register BUFFER *bp;
  100.         register BUFFER *bp1;
  101.         register BUFFER *bp2;
  102.         register int    s;
  103.  
  104.         if ((bp=bfind(bufname, FALSE, 0)) == NULL)      /* Easy if unknown.*/
  105.                 return (TRUE);
  106.         strcpy(lastbuf, curbp->b_bname);
  107.         if (bp->b_nwnd != 0) {                  /* Error if on screen.  */
  108.                 mlwrite("Buffer is being displayed");
  109.                 return (FALSE);
  110.         }
  111.         if ((s=bclear(bp)) != TRUE)             /* Blow text away.      */
  112.                 return (s);
  113.         free((char *) bp->b_linep);             /* Release header line. */
  114.         bp1 = NULL;                             /* Find the header.     */
  115.         bp2 = bheadp;
  116.         while (bp2 != bp) {
  117.                 bp1 = bp2;
  118.                 bp2 = bp2->b_bufp;
  119.         }
  120.         bp2 = bp2->b_bufp;                      /* Next one in chain.   */
  121.         if (bp1 == NULL)                        /* Unlink it.           */
  122.                 bheadp = bp2;
  123.         else
  124.                 bp1->b_bufp = bp2;
  125.         free((char *) bp);                      /* Release buffer block */
  126.         return (TRUE);
  127. }
  128.  
  129. /*
  130.  * List all of the active
  131.  * buffers. First update the special
  132.  * buffer that holds the list. Next make
  133.  * sure at least 1 window is displaying the
  134.  * buffer list, splitting the screen if this
  135.  * is what it takes. Lastly, repaint all of
  136.  * the windows that are displaying the
  137.  * list. Bound to "C-X C-B".
  138.  */
  139. listbuffers(f, n)
  140. register int f, n;
  141. {
  142.         register WINDOW *wp;
  143.         register BUFFER *bp;
  144.         register int    s;
  145.  
  146.         if (blistp == NULL)
  147.                 {
  148.                 blistp = bfind("[List]", TRUE, BFTEMP); /* Buffer list buffer*/
  149.                 if (blistp == NULL)
  150.                         return(ABORT);
  151.                 }
  152.         if ((s=makelist()) != TRUE)
  153.                 return (s);
  154.         if (blistp->b_nwnd == 0) {              /* Not on screen yet.   */
  155.                 if ((wp=wpopup()) == NULL)
  156.                         return (FALSE);
  157.                 bp = wp->w_bufp;
  158.                 if (--bp->b_nwnd == 0) {
  159.                         bp->b_dotp  = wp->w_dotp;
  160.                         bp->b_doto  = wp->w_doto;
  161.                         bp->b_markp = wp->w_markp;
  162.                         bp->b_marko = wp->w_marko;
  163.                 }
  164.                 wp->w_bufp  = blistp;
  165.                 ++blistp->b_nwnd;
  166.         }
  167.         wp = wheadp;
  168.         while (wp != NULL) {
  169.                 if (wp->w_bufp == blistp) {
  170.                         wp->w_linep = lforw(blistp->b_linep);
  171.                         wp->w_dotp  = lforw(blistp->b_linep);
  172.                         wp->w_doto  = 0;
  173.                         wp->w_markp = NULL;
  174.                         wp->w_marko = 0;
  175.                         wp->w_flag |= WFMODE|WFHARD;
  176.                 }
  177.                 wp = wp->w_wndp;
  178.         }
  179.         return (TRUE);
  180. }
  181.  
  182. /*
  183.  * This routine rebuilds the
  184.  * text in the special secret buffer
  185.  * that holds the buffer list. It is called
  186.  * by the list buffers command. Return TRUE
  187.  * if everything works. Return FALSE if there
  188.  * is an error (if there is no memory).
  189.  */
  190. makelist()
  191. {
  192.         register char   *cp1;
  193.         register char   *cp2;
  194.         register int    c;
  195.         register BUFFER *bp;
  196.         register LINE   *lp;
  197.         register long   nbytes;
  198.         register int    s;
  199.         register int    type;
  200.         char            b[6+1];
  201.         char            line[128];
  202.  
  203.         blistp->b_flag &= ~BFCHG;               /* Don't complain!      */
  204.         blistp->b_bmode |= BMNWRAP;
  205.         if ((s=bclear(blistp)) != TRUE)         /* Blow old text away   */
  206.                 return (s);
  207.         strcpy(blistp->b_fname, "");
  208.         if (addline(blistp,"C   Size Buffer           File") == FALSE
  209.         ||  addline(blistp,"-   ---- ------           ----") == FALSE)
  210.                 return (FALSE);
  211.         bp = bheadp;                            /* For all buffers      */
  212.         while (bp != NULL) {
  213.                 if ((bp->b_flag&BFTEMP) != 0) { /* Skip magic ones.     */
  214.                         bp = bp->b_bufp;
  215.                         continue;
  216.                 }
  217.                 cp1 = &line[0];                 /* Start at left edge   */
  218.                 if ((bp->b_flag&BFCHG) != 0)    /* "*" if changed       */
  219.                         *cp1++ = '*';
  220.                 else
  221.                         *cp1++ = ' ';
  222.                 *cp1++ = ' ';                   /* Gap.                 */
  223.                 nbytes = 0L;                    /* Count bytes in buf.  */
  224.                 lp = lforw(bp->b_linep);
  225.                 while (lp != bp->b_linep) {
  226.                         nbytes += llength(lp)+1;
  227.                         lp = lforw(lp);
  228.                 }
  229.                 ltoa(b, 6, nbytes);             /* 6 digit buffer size. */
  230.                 cp2 = &b[0];
  231.                 while ((c = *cp2++) != 0)
  232.                         *cp1++ = c;
  233.                 *cp1++ = ' ';                   /* Gap.                 */
  234.                 cp2 = &bp->b_bname[0];          /* Buffer name          */
  235.